package com.osmino.contacts.groups.edit;

import java.util.List;
import android.app.Dialog;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.osmino.contacts.R;
import com.osmino.contacts.entity.Group;
import com.osmino.contacts.icon.ChooseIconActivity;
import com.osmino.contacts.provider.ContactsContractQueries;
import com.osmino.contacts.provider.ContactsContractResolver;
import com.osmino.contacts.provider.ContactsContractResolver.ContactsDataObserver;
import com.osmino.core.AbsListActivity;
import com.osmino.core.DialogGenerator;
import com.osmino.core.DialogInfo;
import com.osmino.core.GroupEditItemController;
import com.osmino.core.IGroupsEditController;
import com.osmino.core.TextPickerDialog;

public class GroupEditListActivity extends AbsListActivity implements ContactsDataObserver,
		IGroupsEditController, GroupEditItemController {

	private GroupEditAdapter mAdapter;
	private DialogGenerator mDialogGenerator;
	private ImageView mEditIcon, mAddIcon;
	private TextView mTitleBarText;
	private ContactsContractResolver mContactsContractResolver;
	private Group mSelectedGroup;
	private static final int RINGTONE_REQUEST_CODE = 0;
	private static final int ICON_REQUEST_CODE = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContactsContractResolver = mHostApplication.getContactsContractResolver();
		mDialogGenerator = new DialogGenerator(this, this);
		setContentView(R.layout.groups_edit_view);
		mTitleBarText = (TextView) findViewById(R.id.title_bar_text);
		mEditIcon = (ImageView) findViewById(R.id.title_bar_edit_icon);
		mAddIcon = (ImageView) findViewById(R.id.title_bar_add_icon);
		getListView().setVerticalScrollBarEnabled(false);
		mAdapter = new GroupEditAdapter(this, this);
		setListAdapter(mAdapter);
		mEditIcon.setOnClickListener(this);
		mAddIcon.setOnClickListener(this);
		prepTitle();
		mContactsContractResolver.registerContactsDataObserver(this);
		if (mContactsContractResolver.isContactListsReady()) {
			setGroupListToAdapter();
		} else {
			showDialog(R.id.dialog_waiting);
		}
	}

	private void setGroupListToAdapter() {
		List<Group> groups = mContactsContractResolver.getListGroups();
		mAdapter.refresh(groups);
	}

	private void prepTitle() {
		mEditIcon.setVisibility(View.GONE);
		mTitleBarText.setText(R.string.edit_group);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mContactsContractResolver.unregisterContactsDataObserver(this);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case RINGTONE_REQUEST_CODE:
				onRingtoneRequestCallback(resultCode, data);
				break;
			case ICON_REQUEST_CODE:
				onIconRequestCallback(resultCode, data);
				break;
		}
	}

	private void onRingtoneRequestCallback(int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			mSelectedGroup.setRingtone(this, ringtone);
			ContactsContractQueries.editGroup(this, mSelectedGroup);
		}
		mSelectedGroup = null;		
	}
	
	private void onIconRequestCallback(int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			int pickedIcon = data.getIntExtra(ChooseIconActivity.EXTRA_PICKED_ICON, Group.ICON_DEFAULT);
			mSelectedGroup.setIconResource(this, pickedIcon);
		}
		mSelectedGroup = null;			
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
			case R.id.dialog_waiting:
				dialog = mDialogGenerator.createWaitingDialog();
				break;
			case R.id.dialog_edit_group_title:
				dialog = mDialogGenerator.createEditGroupTitleDialog();
				break;
			default:
				dialog = super.onCreateDialog(id);
		}
		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {//
		switch (id) {
			case R.id.dialog_edit_group_title:
				mDialogGenerator.prepareEditGroupTitleDialog((TextPickerDialog) dialog, mSelectedGroup.title);
				break;
		}
	}

	@Override
	public void onDialogCallback(DialogInfo dialogCallbackInfo) {
		switch (dialogCallbackInfo.dialogId) {
			case R.id.dialog_edit_group_title:
				onEditGroupTitle(dialogCallbackInfo);
				break;
		}
	}
	
	private void onEditGroupTitle(DialogInfo dialogCallbackInfo) {
		if (dialogCallbackInfo.whichButton == Dialog.BUTTON_POSITIVE) {
			mSelectedGroup.title = dialogCallbackInfo.text;
			mAdapter.notifyDataSetChanged();
			ContactsContractQueries.editGroup(this, mSelectedGroup);
		}
		mSelectedGroup = null;
	}

	@Override
	public void onAddClick() {
	}

	@Override
	public void onOverviewClick() {
	}

	@Override
	public void onEditClick() {
	}

	@Override
	public void onClick(View v) {
	}

	@Override
	public void onClickDelete(Group info) {
		ContactsContractQueries.deleteGroup(this, info);
	}

	@Override
	public void onClickPic(Group info) {
		mSelectedGroup = info;
		Intent activityCall = new Intent(this, ChooseIconActivity.class);
		activityCall.putExtra(ChooseIconActivity.EXTRA_EXISTING_ICON, mSelectedGroup.getIconResource());
		startActivityForResult(activityCall, ICON_REQUEST_CODE);
	}

	@Override
	public void onClickRingtone(Group info) {
		mSelectedGroup = info;
		Intent activityCall = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
		activityCall.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
				mSelectedGroup.getRingtone());
		startActivityForResult(activityCall, RINGTONE_REQUEST_CODE);
	}

	@Override
	public void onClickEdit(Group info) {
		mSelectedGroup = info;
		showDialog(R.id.dialog_edit_group_title);
	}

	@Override
	public void onClickList(Group info) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStartRefresh() {
		showDialog(R.id.dialog_waiting);
	}

	@Override
	public void onRefreshContactsSctructure() {
		dismissDialog(R.id.dialog_waiting);
		setGroupListToAdapter();
	}
}
